package com.dingli.DAO;

import com.dingli.entity.Person;
import com.dingli.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sound.midi.Track;
import java.sql.SQLException;
import java.util.List;

public class AdminDao {
    private QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSource());
    public List<Person> showAllPerson() throws SQLException {
        String sql = "select * from person";
        List<Person> personList = runner.query(sql, new BeanListHandler<>(Person.class));
        return personList;
    }

    public int deletePerson(String userId) throws SQLException {
        String sql = "delete from person where id = ? and role = 3";
        return runner.update(sql, userId);
    }

    public int updateInfo(int id, String email, String password) throws SQLException {
        String sql = "update person set email = ?, password = ? where id = ?";
        return runner.update(sql, email, password, id);
    }

    public Boolean confirmUser(int id, String oldPassword) throws SQLException {
        String sql = "select * from person where id = ? and password = ?";
        Person p =  runner.query(sql, new BeanHandler<>(Person.class), id, oldPassword);
        return p==null;
    }
}


